#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "usr_info.h"
#include <QListWidget>
#include <QListWidgetItem>

MainWindow::MainWindow(Dialog &dialog, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    socket = dialog.socket;

    memcpy(&usr_data, &dialog.usr_data, DATASIZE);

    memcpy(myname, usr_data.src_id, USERIDSIZE);

    QString Myname = "我的用户名：";
    Myname.append(myname);
    ui->label_3->setText(Myname);
    setWindowTitle("Chat");

    QString OnlineUsernum = "在线用户：";

    ui->label_4->setText(OnlineUsernum);

    onlineuser = 0;

 //   currentmessage = new QString;

    connect(ui->listWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(listWidget_clicked(QModelIndex)));

    connect(socket,&QTcpSocket::readyRead,this,socket_Read_Data);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::socket_Read_Data()
{
    socket->read((char *)&usr_data,DATASIZE);

    if(CMD_USERDATA == usr_data.cmd)
    {
        for(int i=1; i<onlineuser+1; i++)
        {
            QString n = usr_data.src_id;
            if(0 == user_message[i].username.compare(n))
            {
   //             qDebug() << user_message[i].username;
                currentmessage = &user_message[i].message;
   //             qDebug() << *currentmessage;
                break;
            }
        }

        currentmessage->append("\n");
        QString recvdata = usr_data.src_id;
        recvdata.append(":\n   ");
        recvdata.append(usr_data.data);
   //     ui->textEdit->setText(recvdata);

        currentmessage->append(recvdata);
        ui->textEdit->setText(*currentmessage);

        QTextCursor cursor=ui->textEdit->textCursor();
        cursor.movePosition(QTextCursor::End);
        ui->textEdit->setTextCursor(cursor);


    }
    else if(CMD_Onlineuser == usr_data.cmd)//更新在线用户
    {
        QString OnlineUsernum = "在线用户：";
        char buf[10];
        sprintf(buf, "%d", (int)usr_data.dst_id[0]);
        OnlineUsernum.append(buf);
        ui->label_4->setText(OnlineUsernum);

        int num = (int)usr_data.dst_id[0];

        onlineuser = num;

        int offset = 0;
        ui->listWidget->clear();
        for(int i=1; i<num+1; i++)
        {
            user_message[i].username = usr_data.data+offset;
            qDebug() << user_message[i].username;
            QListWidgetItem* lstusername = new QListWidgetItem(usr_data.data+offset, ui->listWidget);
            ui->listWidget->insertItem(i,lstusername);
            offset+=20;
        }
    }
}

void MainWindow::listWidget_clicked(QModelIndex index)
{
    ui->lineEdit->setText(index.data().toString());
    ui->label->setText(index.data().toString().append("："));

    int i = 1;
    for(;i<onlineuser+1;i++)
    {
        if(0 == index.data().toString().compare(user_message[i].username))
        {
 //           qDebug() << user_message[i].username;
            currentmessage = &user_message[i].message;
 //           qDebug() << *currentmessage;
            break;
        }
    }

    ui->textEdit->setText(*currentmessage);
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    usr_data.cmd = CMD_CLOSECHAT;
    socket->write((char *)&usr_data, DATASIZE);
    socket->close();
}

QString MainWindow::getmessage(QString &username)
{

}

void MainWindow::on_pushButton_clicked() /* 发送数据 */
{
    if(ui->lineEdit->text().isEmpty())
    {
        QMessageBox::information(NULL, "错误", "用户名不能为空!");
        return ;
    }

    int i = 1;
    for(;i<onlineuser+1;i++)
    {
        qDebug() << i;
        qDebug() << user_message[i].username;
        if(0 == ui->lineEdit->text().compare(user_message[i].username))
        {
            currentmessage = &user_message[i].message;
            break;
        }
        else if(i == onlineuser)
        {
            QMessageBox::information(NULL, "错误", "用户离线或不存在!");
            return ;
        }
    }

    QString dstname = ui->lineEdit->text();
    QByteArray ba = dstname.toLatin1();
    char *mm = ba.data();

    memset(&usr_data,0,DATASIZE);

    QString str = ui->textEdit_2->toPlainText();
    QByteArray ba1 = str.toUtf8();
    char *mm1 = ba1.data();

    usr_data.cmd = CMD_USERDATA;
    memcpy(usr_data.dst_id, mm, ui->lineEdit->text().length());
    memcpy(usr_data.src_id, myname, USERIDSIZE);
    memcpy(usr_data.data, mm1, str.length());

    currentmessage->append("\n");
    QString recvdata = myname;
    recvdata.append(":\n   ");
    recvdata.append(usr_data.data);
    ui->textEdit->setText(recvdata);

    currentmessage->append(recvdata);
    ui->textEdit->setText(*currentmessage);
    currentmessage->append("\n");

    QTextCursor cursor=ui->textEdit->textCursor();
    cursor.movePosition(QTextCursor::End);
    ui->textEdit->setTextCursor(cursor);

  //  ui->textEdit_2->clear();

    socket->write((char *)&usr_data, DATASIZE);


//    qDebug() << "src_id: " << usr_data.src_id;
//    qDebug() << "dst_id: " << usr_data.dst_id;

}
